package com.qmo.edu.moudle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qmo.edu.R;
import com.qmo.edu.base.BaseMvpActivity;
import com.qmo.edu.moudle.activity.LoginActivity;
import com.qmo.edu.mvp.factroy.CreatePresenterAnnotation;
import com.qmo.edu.mvp.presenter.ForgetPwdPresenter;
import com.qmo.edu.mvp.view.ForgetPwdView;
import com.qmo.edu.utils.FunctionUtils;
import com.qmo.edu.utils.SPUtils;
import com.qmo.edu.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.jessyan.autosize.utils.LogUtils;

/**
 * Created by ${} on 2019/6/2/002.
 *
 * @author cyx
 */

@CreatePresenterAnnotation(ForgetPwdPresenter.class)
public class ForgetPwdActivity extends BaseMvpActivity<ForgetPwdView, ForgetPwdPresenter> implements ForgetPwdView {


    @BindView(R.id.iv_title_return)
    TextView mIvTitleReturn;
    @BindView(R.id.tv_title_text)
    TextView mTvTitleText;
    @BindView(R.id.tv_save)
    TextView mIvSave;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.et_old_pwd)
    EditText mEtOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText mEtNewPwd;


    @Override
    public int setLayoutId() {
        return R.layout.activity_forgot_pwd;
    }

    @Override
    protected void initData() {
        mTvTitleText.setText("修改密码");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mIvSave.setVisibility(View.VISIBLE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        mIvTitleReturn.setVisibility(View.VISIBLE);
        mIvTitleReturn.setTypeface(mIconfont);


    }

    @Override
    public void onGetCode() {
//        startTime(mTvGetCode);
    }

    @Override
    public void onUpdatePwd(String bean) {
//        ChangePwdBean changePwdBean = Json
//        if(bean.getCode() == 0){
        SPUtils.put(ForgetPwdActivity.this, "isLogin", false);
        ToastUtils.showShortToast("修改成功,跳转登陆重新登录");
        Intent intent = new Intent(ForgetPwdActivity.this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();

//            finish();
//        }else {
//            ToastUtils.showShortToast((String) bean.getMessage());
//
//        }
//        Gson gson = new Gson();
//        Toast.makeText(this, bean, Toast.LENGTH_SHORT).show();
        LogUtils.e("-------------------------" + bean);
//        RxBusManager.PostToForgetActivity(new TokenEvent());
//        ChangePwdBean stu2=(ChangePwdBean)JSONObject.toBean(bean, ChangePwdBean.class);
    }


    public void startTime(final TextView tvGetCode) {
        final long codeTimes = 60;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(codeTimes - 1)
                .map(aLong -> codeTimes - aLong)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> tvGetCode.setEnabled(false))
                .compose(this.bindLifeycle())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long value) {
                        tvGetCode.setText("剩余" + value + "秒");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        tvGetCode.setEnabled(true);
                        tvGetCode.setText("获取验证码");
                    }
                });
    }

    @OnClick({R.id.iv_title_return, R.id.tv_save, R.id.tv_message})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_return:
                finish();
                break;
            case R.id.tv_message:

                break;
            case R.id.tv_save:

                if (TextUtils.isEmpty(mEtOldPwd.getText().toString())) {
                    Toast.makeText(this, "请输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtNewPwd.getText().toString())) {
                    Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if(mEtOldPwd.getText().toString().equals(mEtNewPwd.getText().toString())){
                    Toast.makeText(this, "两次密码不能一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("oldPassword", mEtOldPwd.getText().toString());
                map.put("newPassword", mEtNewPwd.getText().toString());
                map.put("confirmPassword", mEtNewPwd.getText().toString());
                getMvpPresenter().updatePwd("Bearer  " + SPUtils.get(ForgetPwdActivity.this, "token", ""), map);

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FunctionUtils.fixInputMethodManagerLeak(ForgetPwdActivity.this);
    }
}
